﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineGameLibrary.Contract;
using OnlineGameClient.ViewModel;

namespace OnlineGameClient.View
{
    public partial class GameConfigView2 : UserControl
    {
        public GameConfigView2()
        {
            InitializeComponent();
        }

        public GameInfo GameInfo
        {
            get { return (GameInfo)GetValue(GameInfoProperty); }
            set { SetValue(GameInfoProperty, value); }
        }
        public static readonly DependencyProperty GameInfoProperty =
            DependencyProperty.Register("GameInfo", typeof(GameInfo), typeof(GameConfigView2),
            new PropertyMetadata(
                new PropertyChangedCallback((o, e) =>
                {
                    var view = o as GameConfigView2;
                    view.DataContext = e.NewValue;
                    view.InitializePanel();
                })));

        bool PraseText(string text, string typestr, out object result)
        {
            var type = Type.GetType(typestr);

            if (type == typeof(string))
            {
                result = text;
                return true;
            }
            else if (type == typeof(int))
            {
                int value;
                bool ret = int.TryParse(text, out value);
                result = value;
                return ret;
            }
            else if (type == typeof(double))
            {
                double value;
                bool ret = double.TryParse(text, out value);
                result = value;
                return ret;
            }
            result = null;
            return false;
        }

        public void SetGameConfig(GameConfig gameconfig)
        {
            var valuelist = gameconfig.ConfigDict.Values.ToArray();
            for (int i = 0; i < configpanel.Children.Count; i++)
            {
                var configdef = GameInfo.GameConfigDefines[i];
                var field = (DataField)configpanel.Children[i];

                ((TextBox)field.Content).Text = valuelist[i].ToString();

            }
        }

        public GameConfig GetGameConfig()
        {
            if (GameInfo != null)
            {
                GameConfig gameconfig = new GameConfig();
                gameconfig.GameIdentityName = GameInfo.IdentityName;

                erroinfotb.Text = null;
                for (int i = 0; i < configpanel.Children.Count; i++)
                {
                    var configdef = GameInfo.GameConfigDefines[i];
                    var field = (DataField)configpanel.Children[i];

                    var text = ((TextBox)field.Content).Text;

                    object value;
                    var result = PraseText(text, configdef.ConfigValueType, out value);
                    if (!result)
                    {
                        erroinfotb.Text += configdef.ConfigName + " value invalid\n";
                    }
                    else
                        gameconfig.ConfigDict[configdef.ConfigName] = value;
                }
                return gameconfig;
            }
            else
                return null;
        }

        public bool ValidateConfig()
        {
            if (GameInfo != null)
            {
                erroinfotb.Text = null;
                for (int i = 0; i < configpanel.Children.Count; i++)
                {
                    var configdef = GameInfo.GameConfigDefines[i];
                    var field = (DataField)configpanel.Children[i];

                    var text = ((TextBox)field.Content).Text;

                    object value;
                    var result = PraseText(text, configdef.ConfigValueType, out value);
                    if (!result)
                    {
                        erroinfotb.Text += configdef.ConfigName + " value invalid\n";
                    }
                }

                if (string.IsNullOrEmpty(erroinfotb.Text))
                    return true;
                else
                    return false;
            }
            return false;
        }


        public void InitializePanel()
        {
            if (GameInfo != null)
            {
                mintb.Text = GameInfo.MinPlayerNumber.ToString();
                maxtb.Text = GameInfo.MaxPlayerNumber.ToString();

                configpanel.Children.Clear();
                foreach (var configdefine in GameInfo.GameConfigDefines)
                {
                    DataField field = new DataField();
                    field.MaxWidth = 400;
                    field.Height = 28;
                    field.Label = configdefine.ConfigName;
                    field.Description = configdefine.Description;
                    field.DescriptionViewerPosition = DataFieldDescriptionViewerPosition.BesideLabel;

                    TextBox innertb = new TextBox();
                    innertb.Width = 200;
                    innertb.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    innertb.Text = configdefine.DefaultValue.ToString();

                    field.Content = innertb;
                    configpanel.Children.Add(field);
                }
            }
        }
    }
}
